using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Templates_Article_Normal : BaseTemplate
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //find topic from post
            System.Collections.Generic.List<Acms.Bll.PostTopic> tmp = Acms.Bll.PostTopic.GetAllByPostId(CurrentPost.Id);
            if (tmp != null && tmp.Count > 0)
            {
                selectedTopicId = tmp[0].TopicId;
            }
            else
            {
                dlTopics.SelectedIndex = 0;
            }
        }
    }

    protected void dlTopics_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
        if (e.Item.IsSelectedItem)
        {
            Acms.Bll.Topic topic = e.Item.DataItem as Acms.Bll.Topic;
            HyperLink lnkTopic = e.Item.FindControl("lnkTopic") as HyperLink;
            HyperLink lnkIssue = e.Item.FindControl("lnkIssue") as HyperLink;
            if (topic != null && lnkTopic != null)
            {
                lnkTopic.NavigateUrl = string.Format("{0}?SelectedTopic={1}", CurrentPost.ParentUrl, topic.Id);
            }
            if (topic != null && lnkIssue != null)
            {
                lnkIssue.NavigateUrl = string.Format("{0}?SelectedTopic={1}", CurrentPost.ParentUrl, topic.Id);
            }
        }
        else if (e.Item.IsNormalItem)
        {
            Acms.Bll.Topic topic = e.Item.DataItem as Acms.Bll.Topic;
            HyperLink lnkTopic = e.Item.FindControl("lnkTopic") as HyperLink;
            if (topic != null && lnkTopic != null)
            {
                lnkTopic.NavigateUrl = string.Format("{0}?SelectedTopic={1}", CurrentPost.ParentUrl, topic.Id);
            }
        }
    }

    private int selectedTopicId;

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        System.Collections.Generic.List<Acms.Bll.Topic> tmp = Acms.Bll.Topic.GetAllBySiteId(CurrentSiteId);
        if (selectedTopicId > 0)
        {
            foreach (Acms.Bll.Topic t in tmp)
            {
                if (t.Id == selectedTopicId)
                {
                    dlTopics.SelectedIndex = tmp.IndexOf(t);
                    break;
                }
            }
        }
        e.SetData(tmp);
    }
}